﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Infrastructure.Common;
using Xiaov.SmartWeb.Model.ActiveRecord;
using Xiaov.SmartWeb.Model;


namespace Xiaov.SmartWeb.Application.PluginSrc.GuestBook
{
    public partial class ViewSubject : BasePage
    {
        public string Id { get; set; }

        public A_GUESTBOOK_SUBJECT Subject { get; set; } 

        public IList<A_GUESTBOOK_COMMENT> Comments { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            PM_FilterByProperty(ModulePropoty.MODULEURL, "/PluginSrc/GuestBook/SubjectList.aspx");
            if (!IsPostBack)
            {
                Id = Request.QueryString["id"].ToRequestString();
                BindData();
            }
        }

        private void BindData()
        {
            Subject = new A_GUESTBOOK_SUBJECT();
            Subject = A_GUESTBOOK_SUBJECT.TryFind(Id);
            if (Subject != null)
            {
                Comments = Subject.Comments;
            }
            else
            {
                General.ShowAlert("主题已经被删除或不存在");
            }
        }

        protected void btn_Reply_Click(object sender, EventArgs e)
        {
            if (Permissions[1].Allow)
            {
                Id = Request.QueryString["id"].ToRequestString();
                if (A_GUESTBOOK_SUBJECT.TryFind(Id) != null)
                {
                    A_GUESTBOOK_COMMENT comment = new A_GUESTBOOK_COMMENT();
                    comment.REPLYID = Guid.NewGuid().ToString();
                    comment.SUBJECTID = Id;
                    comment.REPLYAUTHOR = PUser.USERNAME;
                    comment.REPLYTIME = DateTime.Now;
                    comment.REPLYCONTENT = Request.Form["ctl00$ContentPlaceHolder1$t_content"].ToRequestString();
                    comment.Save();
                    comment.Refresh();
                    Response.Redirect("ViewSubject.aspx?id=" + Id, true);
                }
            }
            else
            {
                General.ShowAlert("没有授权！");
            }
        }
    }
}